RippleBusLibrary{
	
	classvar startBus = 20, lastReservedBus, <busesInUse, numAudioBusChannels, inUseBusesNotOnTimer,logger;
	
	*init{
		busesInUse = List.new;	
		inUseBusesNotOnTimer = List.new;	
		lastReservedBus = startBus; 
		logger = RippleLogger.new;
		logger.fileName = "RippleBusLibrary.log"
	}
	
	*checkoutBus{|time|
	
		
		numAudioBusChannels = Server.default.options.numAudioBusChannels;
		
		if (busesInUse == nil){
			this.init;
		};
		
		logger.log("checkoutBus");
		logger.log("busesInUse: " + busesInUse);
		
		numAudioBusChannels.do{|i|
			if(
				i > lastReservedBus 
				&& (i % 2 == 0) 
				&& (busesInUse.includes(i) == false),
				{
					busesInUse.add(i);
					if(time.notNil, {
						AppClock.sched(time, {busesInUse.remove(i);});
						},{
							inUseBusesNotOnTimer.add(i);	
						}
					);
					this.setLastReservedBus(i);
					logger.log("checking out bus number: " + i);
					^i;
					
				}
			
			);
		};
			
		"************** WARNING: USING TOO MANY AUDIO BUSES *********".postln;
		^0
		
	}
	
	*setLastReservedBus{|i|
		if(
			(i <= (numAudioBusChannels - 5)), // sloppy. don't know the exact number
			{lastReservedBus = i},
			{lastReservedBus = startBus}
		);
	
	}
	
	*lendBus{|number, time|
		busesInUse.add(number);
		AppClock.sched(time, {busesInUse.remove(number);});
		^number;
	}
	
	*returnBus{|busNum|
		busesInUse.remove(busNum);	
	}
	
}